var express = require('express');
var router = express.Router();
var { userModel, roleModel, menusModel } = require('../model/model')

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/admenus', (req, res) => {
  menusModel.create(req.body)
  res.send({ code: 200 })
})

router.get('/getmenus', async (req, res) => {
  let menusLs = await menusModel.find().lean()
  let menu = {}
  menusLs.forEach(el => {
    menu[el._id] = el
  })

  let ls = []
  menusLs.forEach(el => {
    if (!el['uid']) {
      ls.push(el)
    } else {
      if (!menu[el.uid].children) {
        menu[el.uid]['children'] = []
      }
      menu[el.uid].children.push(el)
    }
  })
  res.send({ code: 200, msg: '权限列表获取成功', ls })
})


module.exports = router;
